package com.zzzzzz.content.reply;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzzzzz.account.signin.OnlineService;
import com.zzzzzz.common.GlobalConstants;
import com.zzzzzz.common.message.Message;
import com.zzzzzz.common.message.MessageIcon;
import com.zzzzzz.content.Constants;

@Controller
public class ReplyController {
	
	@Resource
	private ReplyService replyService;
	@Resource
	private OnlineService onlineService;
	
	@RequestMapping(value = "/content/reply/{topicId}", method = RequestMethod.POST)
	public @ResponseBody Message add(@PathVariable Long topicId, @RequestParam String text) throws Exception {
		if(StringUtils.isBlank(text)){
			return Message.warning(MessageIcon.WARNING + "请填写评论内容");
		}else{
			Long userId = onlineService.findUserId();
			
			Date now = new Date();
			Reply reply = new Reply();
			reply.setTopicId(topicId);
			reply.setTypeId(Constants.CONTENT_DB_TYPE.CONTENT);
			reply.setText(text);
			reply.setAddDt(now);
			reply.setUpdDt(now);
			reply.setUserId(userId);
			reply.setStatus(GlobalConstants.STATUS.NORMAL);
			
			replyService.add(reply);
			
			
			long count = replyService.findByTopicIdCount(topicId, Constants.CONTENT_DB_TYPE.CONTENT);
			ReplyMsgView replyMsgView = new ReplyMsgView(reply.getText(), reply.getAddDt(), count);
			
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
			String json = objectMapper.writeValueAsString(replyMsgView);
			return Message.success(MessageIcon.OK + "已评论成功", json);
		}
	}
	
	@RequestMapping(value = "/content/reply/picitem/{topicId}", method = RequestMethod.POST)
	public @ResponseBody Message addPicItem(@PathVariable Long topicId, @RequestParam String text) throws Exception {
		if(StringUtils.isBlank(text)){
			return Message.warning(MessageIcon.WARNING + "请填写评论内容");
		}else{
			Long userId = onlineService.findUserId();
			
			Date now = new Date();
			Reply reply = new Reply();
			reply.setTopicId(topicId);
			reply.setTypeId(Constants.CONTENT_DB_TYPE.PICITEM);
			reply.setText(text);
			reply.setAddDt(now);
			reply.setUpdDt(now);
			reply.setUserId(userId);
			reply.setStatus(GlobalConstants.STATUS.NORMAL);
			
			replyService.add(reply);
			
			
			long count = replyService.findByTopicIdCount(topicId, Constants.CONTENT_DB_TYPE.PICITEM);
			ReplyMsgView replyMsgView = new ReplyMsgView(reply.getText(), reply.getAddDt(), count);
			
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
			String json = objectMapper.writeValueAsString(replyMsgView);
			return Message.success(MessageIcon.OK + "已评论成功", json);
		}
	}
	
	class ReplyMsgView{
		private String text;
		private Date addDt;
		private long count;
		
		public ReplyMsgView(String text, Date addDt, long count) {
			this.text = text;
			this.addDt = addDt;
			this.count = count;
		}
		
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		public Date getAddDt() {
			return addDt;
		}
		public void setAddDt(Date addDt) {
			this.addDt = addDt;
		}
		public long getCount() {
			return count;
		}
		public void setCount(long count) {
			this.count = count;
		}
	}
	
}
